use std::collections::HashMap;
use rpc::dto::{RpcData, RpcExtData, RpcHeader};
use rpc::{decode, decode_rpc_header, encode, encode_rpc_data, RpcContext, RpcResult};

#[test]
fn test() {
    let tt = RpcData::new("dd", 12);
}

#[test]
fn codec_test_json()-> RpcResult<()> {
    let header = RpcHeader {
        path: "login".to_string(),
        version: 10,
        status: 110,
        ext_data: RpcExtData::new(HashMap::new()),
    };

    let context = RpcContext::test_context_with_codec_type("json");
    let data = RpcData::build(header, "hello".to_string());
    let mut buff = encode_rpc_data(&context, &data)?;
    let buff = buff.split_off(4);
    let (header, dd) = decode_rpc_header(&context, &buff)?;
    let value = decode::<String>(&context, dd)?;

    assert_eq!(&header.path, &"login".to_string());
    assert_eq!(&header.status, &110);
    Ok(())
}

#[test]
fn codec_test_binary()-> RpcResult<()> {
    let header = RpcHeader::new("login", "demo".to_string(), 10);;

    let context = RpcContext::test_context_with_codec_type("bin-code");
    let data = encode(&context, &"hello".to_string())?;
    let data = RpcData::build(header, data);
    let buff = encode_rpc_data(&context, &data)?;
    let (header, dd) = decode_rpc_header(&context, &buff[4..].to_vec())?;

    assert_eq!(&header.path, &"login".to_string());
    assert_eq!(&header.status, &10);
    Ok(())
}